***Tracker***

use "${original}TRK2014TR_R",clear
replace HHID=OVHHID if OVRESULT==1
replace PN=OVPN if OVRESULT==1
sort HHID PN

*** --- copy and clean variable labels --- ***
qui foreach v of var * { 
	local l`v' : variable label `v'
	local lA`v': subinstr local l`v' "1992 " "",all
	local lA2`v': subinstr local lA`v' " 1992" "",all
	local lB`v': subinstr local lA2`v' "1993 " "",all
	local lB2`v': subinstr local lB`v' " 1993" "",all
	local lC`v': subinstr local lB2`v' "1994" "",all
	local lC2`v': subinstr local lC`v' "1994" "",all
	local lD`v': subinstr local lC2`v' "1995" "",all
	local lD2`v': subinstr local lD`v' "1995" "",all
	local lE`v': subinstr local lD2`v' "1996" "",all
	local lE2`v': subinstr local lE`v' "1996" "",all
	local lF`v': subinstr local lE2`v' "1998" "",all
	local lF2`v': subinstr local lF`v' "1998" "",all
	local lG`v': subinstr local lF2`v' "2000" "",all
	local lG2`v': subinstr local lG`v' "2000" "",all
	local lH`v': subinstr local lG2`v' "2002" "",all
	local lH2`v': subinstr local lH`v' "2002" "",all
	local lJ`v': subinstr local lH2`v' "2004" "",all
	local lJ2`v': subinstr local lJ`v' "2004" "",all
	local lK`v': subinstr local lJ2`v' "2006" "",all
	local lK2`v': subinstr local lK`v' "2006" "",all
	local lL`v': subinstr local lK2`v' "2008" "",all
	local lL2`v': subinstr local lL`v' "2008" "",all
	local lM`v': subinstr local lL2`v' "2010" "",all
	local lM2`v': subinstr local lM`v' "2010" "",all
	local lN`v': subinstr local lM2`v' "2012" "",all
	local lN2`v': subinstr local lN`v' "2012" "",all
	local lO`v': subinstr local lN2`v' "2014" "",all
	local lO2`v': subinstr local lO`v' "2014" "",all
}

*** --- rename variables to accomodate reshape --- ***
drop *99 *03 *04 *05 *06 *07 *08 *09 *10 *11 *12 *13 *14 *15
ren (A* B* C* D* E* F* G* H* J* K* L* M* N* O*) (*1992 *1993 *1994 *1995 *1996 *1998 *2000 *2002 *2004 *2006 *2008 *2010 *2012 *2014)

local oth "HID2002 PN IRTHMO1993 IRTHYR1993 EGREE1995 FTFASSIGN1996 XDEATHMO1996 XDEATHYR1996 XDODSOURCE1996 IRSTIW1998 ENDER2000 ISPANIC2002 IMMGYEAR OSFLAG2010 MONTH2012 SCORE2012 YEAR2012 VHHID2014 VPN2014 VRESULT2014 RACE SCHLYRS SECU STRATUM STUDY USBORN WTCOHORT YRENTER DAMS11992 AMS011994 UMS012002 VERSION"

*** --- store unique stub names throughout all waves in macro to accomodate reshape --- ***
unab waves: *1992 *1993 *1994 *1995 *1996 *1998 *2000 *2002 *2004 *2006 *2008 *2010 *2012 *2014
local waves1: list waves - oth
local wavesA: subinstr local waves1 "1992" "",all
local wavesB: subinstr local wavesA "1993" "",all
local wavesC: subinstr local wavesB "1994" "",all
local wavesD: subinstr local wavesC "1995" "",all
local wavesE: subinstr local wavesD "1996" "",all
local wavesF: subinstr local wavesE "1998" "",all
local wavesG: subinstr local wavesF "2000" "",all
local wavesH: subinstr local wavesG "2002" "",all
local wavesJ: subinstr local wavesH "2004" "",all
local wavesK: subinstr local wavesJ "2006" "",all
local wavesL: subinstr local wavesK "2008" "",all
local wavesM: subinstr local wavesL "2010" "",all
local wavesN: subinstr local wavesM "2012" "",all
local wavesO: subinstr local wavesN "2014" "",all
local stubs: list uniq wavesO

ren HID2002 HHID

qui reshape long `stubs', i(HHID PN) j(YEAR) string

ren (*1992 *1993 *1994 *1995 *1996 *1998 *2000 *2002 *2010 *2012 *2014) (A* B* C* D* E* F* G* H* M* N* O*)

*** --- relabel variables that lost label in reshape --- ***
foreach v of var * { 
	local L`v' : variable label `v'
		if `"`L`v''"' == "" & `"`lO2`v''"' != "" { 
			label var `v' `"`lO2`v''"' 
		}
}
foreach v of var * { 
	foreach y in A B C D E F G H J K L M N O {	
		local L`v' : variable label `v'
			if `"`L`v''"' == "" & `"`lO2`y'`v''"' != "" { 
				label var `v' `"`lO2`y'`v''"' 
			}
	}
}
foreach v of var * { 
	foreach y in 04 06 08 10 12 14 {	
		local L`v' : variable label `v'
			if `"`L`v''"' == "" & `"`lO2`v'`y''"' != "" { 
				label var `v' `"`lO2`v'`y''"' 
			}
	}
}
rename *, lower

lab var year "WAVE"
destring year,replace
save trk2014, replace

use  sample,clear
merge 1:1 hhid pn year using trk2014,gen(trk2014) keep(1 3) keepusing(subhh) // there is one overlap case that only responds to wave 1 that is dropped with this procedure since it does not merge with the tracker file. See the documentation in randhrs_P.pdf
destring hhid pn,replace
lab var trk2014 "_merge tracker file"
save sample_wgt,replace

*Erase temporary datasets
erase trk2014.dta
erase sample.dta